package com.andy.buyu.server.socket;

import java.util.Collection;
import java.util.List;

import com.andy.buyu.server.dto.AttackMessage;
import com.andy.buyu.server.dto.BatteryAttrMessage;
import com.andy.buyu.server.dto.CatchMessage;
import com.andy.buyu.server.dto.ChangeBatteryMessage;
import com.andy.buyu.server.dto.ChangeRateMessage;
import com.andy.buyu.server.dto.ChatMessage;
import com.andy.buyu.server.dto.ConfsMessage;
import com.andy.buyu.server.dto.EnterGameMessage;
import com.andy.buyu.server.dto.EnterRoomMessage;
import com.andy.buyu.server.dto.ErrorMessage;
import com.andy.buyu.server.dto.FishMessage;
import com.andy.buyu.server.dto.GameInfoMessage;
import com.andy.buyu.server.dto.HeartBeatMessage;
import com.andy.buyu.server.dto.LabaMessage;
import com.andy.buyu.server.dto.LockAttackMessage;
import com.andy.buyu.server.dto.MultCatchMessage;
import com.andy.buyu.server.dto.NextStagMessage;
import com.andy.buyu.server.dto.NoticeMessage;
import com.andy.buyu.server.dto.OutGameMessage;
import com.andy.buyu.server.dto.RoomMessage;
import com.andy.buyu.server.dto.RoomRewardMessage;
import com.andy.buyu.server.dto.UpdateRoomMessage;
import com.andy.buyu.server.dto.UseBatteryMessage;
import com.andy.buyu.server.dto.UserCoinMessage;
import com.andy.buyu.server.dto.UserEnterMessage;
import com.andy.buyu.server.dto.UserMessage;
import com.andy.buyu.server.entity.ConfScript;
import com.andy.buyu.server.entity.UsrGameLog;
import com.andy.buyu.server.game.FishGame;
import com.andy.buyu.server.game.GameUser;
import com.andy.buyu.server.game.Global;
import com.andy.buyu.server.game.Room;
import com.andy.buyu.server.socket.codec.IWrite;

public class SendUtil {

	public static void send100(MySocketSession session,GameUser gameUser) {
		UserMessage um = new UserMessage(gameUser);
		doSend(session,GmAction.ACTION_100, um);
	}
	
	public static void sendError(MySocketSession session,int code) {
		ErrorMessage um = new ErrorMessage(code);
		doSend(session,GmAction.ACTION_101, um);
	}
	
	public static void send102(GameUser user) {
		RoomMessage um = new RoomMessage();
		doSend(user,GmAction.ACTION_102, um);
	}
	
	public static void send103(Room room,int index) {
		UpdateRoomMessage um = new UpdateRoomMessage(room,index);
		Collection<MySocketSession> sessions = Global.getUserSessions();
		for(MySocketSession se:sessions) {
			GameUser seu = se.getUser();
			if(seu != null && seu.getRoom() == null) {
				// 大厅人员
				doSend(se,GmAction.ACTION_103, um);
				
			}
		}
	}
	
	public static void send110(MySocketSession session,int desk, int index) {
		EnterRoomMessage um = new EnterRoomMessage(desk,index);
		doSend(session,GmAction.ACTION_110, um);
	}
	
	public static void send111(MySocketSession session,GameUser gameUser) {
		EnterGameMessage um = new EnterGameMessage(gameUser);
		doSend(session,GmAction.ACTION_111, um);
	}
	
	public static void send112(GameUser gameUser) {
		UserEnterMessage um = new UserEnterMessage(gameUser);
		GameUser[] users = gameUser.getRoom().getUsers();
		for(GameUser ur:users){
			if(gameUser != ur){
				doSend(ur,GmAction.ACTION_112, um);
			}
		}
	}
	
	public static void send113(MySocketSession session,GameUser gameUser) {
		GameInfoMessage um = new GameInfoMessage(gameUser);
		doSend(session,GmAction.ACTION_113, um);
	}
	
	public static void send114(MySocketSession session,GameUser gameUser) {
		UserCoinMessage um = new UserCoinMessage(gameUser);
		doSend(session,GmAction.ACTION_114, um);
	}
	
	public static void send118(Room room,GameUser gameUser) {
		if(room == null) return;
		OutGameMessage um = new OutGameMessage(gameUser);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_118, um);
		}
		doSend(gameUser, GmAction.ACTION_118, um);
	}
	
	
	
	
	public static void send200(Room room,GameUser user, int battery,int lostCoin, int x, int y) {
		AttackMessage um = new AttackMessage(user,battery,lostCoin,x,y);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_200, um);
		}
	}
	
	public static void send201(Room room,ConfScript script) {
		FishMessage um = new FishMessage(script);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_201, um);
		}
	}
	
	public static void send202(Room room,int bullet,UsrGameLog log, GameUser gameUser) {
		CatchMessage um = new CatchMessage(log,bullet,gameUser);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_202, um);
		}
	}
	
	public static void send203(GameUser gameUser) {
		ChangeBatteryMessage um = new ChangeBatteryMessage(gameUser);
		GameUser[] users = gameUser.getRoom().getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_203, um);
		}
	}
	
	public static void send204(GameUser gameUser) {
		ChangeRateMessage um = new ChangeRateMessage(gameUser);
		GameUser[] users = gameUser.getRoom().getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_204, um);
		}
	}
	
	public static void send205(Room room, FishGame fishGame) {
		NextStagMessage um = new NextStagMessage(fishGame);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_205, um);
		}
	}
	
	public static void send206(Room room, GameUser gameUser,int lostCoin,int scriptId) {
		LockAttackMessage um = new LockAttackMessage(gameUser,lostCoin,scriptId);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_206, um);
		}
	}
	
	public static void send207(Room room,GameUser gameUser,int bullet, List<UsrGameLog> logs, GameUser user) {
		if(logs.isEmpty()) return;
		MultCatchMessage um = new MultCatchMessage(gameUser.getId(), bullet,logs,user);
		GameUser[] users = room.getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_207, um);
		}
	}
	
	public static void send208(GameUser gameUser,UsrGameLog log) {
		LabaMessage um = new LabaMessage(gameUser, log);
		doSend(gameUser, GmAction.ACTION_208, um);
	}
	
	public static void send209(MySocketSession session,Room room) {
		RoomRewardMessage um = new RoomRewardMessage(room);
		doSend(session, GmAction.ACTION_209, um);
	}
	
	public static void send210(MySocketSession session,GameUser gameUser) {
		UseBatteryMessage um = new UseBatteryMessage(gameUser);
		if(session == null) {
			doSend(gameUser, GmAction.ACTION_210, um);
		}else {
			doSend(session, GmAction.ACTION_210, um);
		}
	}
	
	public static void send211(GameUser gameUser,int attr,int val) {
		BatteryAttrMessage um = new BatteryAttrMessage(attr,val);
		doSend(gameUser, GmAction.ACTION_211, um);
	}
	
	
	public static void send131(GameUser gameUser,String msg) {
		ChatMessage um = new ChatMessage(gameUser,msg);
		GameUser[] users = gameUser.getRoom().getUsers();
		for(GameUser ur:users){
			doSend(ur,GmAction.ACTION_131, um);
		}
	}
	
	public static void send132(String msg) {
		NoticeMessage um = new NoticeMessage(msg);
		broadcast(GmAction.ACTION_132, um);
	}
	
	
	
	public static void send300(MySocketSession session, int version) {
		ConfsMessage um = new ConfsMessage(version);
		doSend(session,GmAction.ACTION_300, um);
	}
	
	public static void send400(MySocketSession session) {
		doSend(session,GmAction.ACTION_400, new HeartBeatMessage());
	}
	
	/**
	 * 发送信息
	 * @param sessionOrId 可以是session，gameUser,uid
	 * @param action
	 * @param data
	 */
	private static void doSend(Object sessionOrId,int action,IWrite data) {
		if(data == null)return;
		if(sessionOrId != null){
			MySocketSession sess = null;
			if(sessionOrId instanceof MySocketSession){
				sess = (MySocketSession) sessionOrId;
			}else if(sessionOrId instanceof GameUser){
				sess = Global.getSession(((GameUser) sessionOrId).getId());
			}else{
				sess = Global.getSession((Long) sessionOrId);
			}
			if(sess != null){
				data.setCmd(action);
				sess.sendMessage(data);
			}
		}
	}
	
	public static void broadcast(int action,IWrite data) {
		if(data == null)return;
		for(MySocketSession session:Global.getUserSessions()){
			doSend(session,action,data);
		}
	}


}
